package com.yearno.common.utils

import java.text.SimpleDateFormat
import java.util.*

object DateUtil {

    fun date2String(date: Long): String {
        return SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date(date))
    }

    fun date2String(date: Date): String {
        return SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(date)
    }

    fun date2String2(date: Date): String {
        return SimpleDateFormat("yyyy-MM", Locale.getDefault()).format(date)
    }

    fun date2StringDay(date: Long): Int? {
        return SimpleDateFormat("dd", Locale.getDefault()).format(Date(date)).toIntOrNull()
    }

    /**
     *
     * 将秒数转化为时分秒
     * @param sec 时间段对应的秒数
     * @return 时分秒格式时间戳
     */
    fun secToTime(sec: Int): String {
        var time = sec
        var hour = 0
        var minute = 0
        var second = 0
        if (time >= 3600) {
            hour = time / 3600
            time %= 3600
        }
        if (time >= 60) {
            minute = time / 60
            time %= 60
        }
        if (time < 60) {
            second = time
        }
        return timeFormat(hour) + ":" + timeFormat(minute) + ":" + timeFormat(second)
    }

    fun secToTimeCN(sec: Int): String {
        var time = sec
        var hour = 0
        var minute = 0
        var second = 0
        if (time >= 3600) {
            hour = time / 3600
            time %= 3600
        }
        if (time >= 60) {
            minute = time / 60
            time %= 60
        }
        if (time < 60) {
            second = time
        }
        return timeFormat(hour) + "小时" + timeFormat(minute) + "分"
    }

    private fun timeFormat(num: Int): String {
        return if (num in 0..9) {
            "0$num"
        } else {
            "" + num
        }
    }
}